package beans;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Singleton;
import javax.ejb.Startup;

import model.AgentInfo;
import model.Host;
import webservice.NodeCommunicationWS;
import agents.Agent;
import beans.interfaces.AgentDataLocal;

/**
 * Session Bean implementation class AgentData
 */
@Singleton
@LocalBean
@Startup
public class AgentData implements AgentDataLocal {

	private HashMap<String, Agent> registeredAgents;
	private HashMap<AgentInfo,Agent> registeredAgents2;
	private HashSet<Host> hosts = new HashSet<Host>();
	private Host localHost;
	NodeCommunicationWS service;
	
	@EJB
	private ConnectionManagerLocal cm;
	
    /**
     * Default constructor. 
     */
    public AgentData() {
        // TODO Auto-generated constructor stub
    }

    @PostConstruct
	@Override
	public void init() {

		
		// Ucitavanje adrese iz konfiguracionog fajla
		/////////////////////////////////////////////////////////////////////////////////
		//localHost = cm.getLocalhost();
		//hosts.add(localHost);
		registeredAgents = new HashMap<String, Agent>();
		registeredAgents2 = new HashMap<AgentInfo, Agent>();
		/*
		registeredAgents.put("master1", new MasterAgent(new AgentInfo(localHost, AgentType.MASTER,666)));
		registeredAgents.put("merchant1", new MerchantAgent(localHost));
		//registeredAgents.put("merchant2", new MerchantAgent());
		 */
		///////////////////////////////////////////////////////////////////////////////////
		// registracija kod ostalih cvorova

		
    }
    
    @Override
    public void setLocalhost(Host localhost) {
    	if(localhost!=null)
    		this.localHost=localhost;
    }
    
    @Override
    public void addHost(Host h) {
    	if(!hosts.contains(h)) {
    		hosts.add(h);
    	}
    }
    
    @Override
    public void addHosts(Collection<Host> remoteHostList) {
    	if(remoteHostList!=null && !remoteHostList.isEmpty()) {
    		hosts.addAll(remoteHostList);
    	}
    }
    
	@Override
	public ArrayList<Agent> getAllAgents() {
		return new ArrayList<Agent>(registeredAgents.values());
	}

	@Override
	public ArrayList<Host> getHosts() {
		// TODO Auto-generated method stub
		return new ArrayList<Host>(hosts);
	}

	@Override
	public Host getLocalHost() {
		// TODO Auto-generated method stub
		return localHost;
	}

	@Override
	public Agent getAgent(String key) {
		// TODO Auto-generated method stub
		if(registeredAgents==null)
			return null;
		return registeredAgents.get(key);
	}
	
	@Override
	public Agent getAgent(AgentInfo info) {
		if(registeredAgents2==null)
			return null;
		return registeredAgents2.get(info);
	}
	
	@Override
	public Agent getAgent(Agent agent) {
		return registeredAgents2.get(agent.getAgentInfo());
	}
	
	@Override
	public HashMap<String,Agent> getAgents() {
		return registeredAgents;
	}

	@Override
	public void addAgent(Agent agent, String key) {
		// TODO Auto-generated method stub
		if(key==null) {
			key=agent.getAgentInfo().getAgentType()+String.valueOf(agent.getAgentInfo().getAID());
		}
		registeredAgents.put(key, agent);
		registeredAgents2.put(agent.getAgentInfo(), agent);
		//cm.syncAgentsWithOtherNodes();
	}
	
	@Override
	public void addAgents(HashMap<String,Agent> agents) {
		for(String key : agents.keySet()) {
			if(!registeredAgents.containsKey(key)) {
				if(!registeredAgents.containsValue(agents.get(key))) {
					addAgent(agents.get(key), key);
				}
			}
		}
	}

	@Override
	public Agent removeAgent(Agent agent) {
		// TODO Auto-generated method stub
		registeredAgents.values().remove(agent);
		//cm.syncAgentsWithOtherNodes();
		return registeredAgents2.remove(agent.getAgentInfo());
	}

	@Override
	public Agent removeAgent(String key) {
		// TODO Auto-generated method stub
		Agent a = registeredAgents.remove(key);
		registeredAgents2.remove(a.getAgentInfo());
		//cm.syncAgentsWithOtherNodes();
		return a;
	}
	
	@Override
	public ArrayList<Agent> removeAllAgents(ArrayList<Agent> agentsToBeRemoved) {
		ArrayList<Agent> removedAgents = new ArrayList<Agent>();
		for(Agent a : agentsToBeRemoved) {
			if(a!=null && getAgent(a)!=null) {
				removeAgent(a);
				removedAgents.add(a);
			}
		}
		return removedAgents;
	}
}
